package com.onpositive.picture.utils;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.onpositive.datamap.core.IVideoObject;

public class YoutubeFeed {
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static ArrayList<IVideoObject> build(String topic) {
		topic=topic.replaceAll(" ","+");
		String uri = "http://gdata.youtube.com/feeds/api/videos?vq="+topic+"";
		try {
			InputStream content = new URL(uri).openStream();
			Document parse = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder().parse(content);
			return (ArrayList)new YoutubeFeed(parse).items;
		} catch (Exception e) {
			// FIX ME
			return new ArrayList();
		}
	}

	static class YoutubeItem implements IVideoObject  {

		protected String title;
		protected String description;
		protected double rating;
		protected int statistics;
		public String getTitle() {
			return title;
		}
		public String getDescription() {
			return description;
		}
		public double getRating() {
			return rating;
		}
		public int getStatistics() {
			return statistics;
		}
		public double getDuration() {
			return duration;
		}
		public String getAuthor() {
			return author;
		}
		public String getSubmitted() {
			return submitted;
		}
		public String getVideoUrl() {
			return videoUrl;
		}
		public String getThumbnailUrl() {
			return thumbnailUrl;
		}
		protected double duration;
		protected String author;
		protected String submitted;
		protected String videoUrl;
		protected String thumbnailUrl;
	}

	ArrayList<YoutubeItem> items = new ArrayList<YoutubeFeed.YoutubeItem>();

	public YoutubeFeed(Document parse) {
		NodeList childNodes = parse.getDocumentElement().getChildNodes();
		for (int a = 0; a < childNodes.getLength(); a++) {
			Node item = childNodes.item(a);
			if (item instanceof Element) {
				visitChild(item);
			}
		}
	}

	protected void visitChild(Node item) {
		if (item.getNodeName().equals("entry")) {
			YoutubeItem it = new YoutubeItem();
			NodeList childNodes = item.getChildNodes();
			for (int a = 0; a < childNodes.getLength(); a++) {
				Node node = childNodes.item(a);
				if (node instanceof Element) {
					if (node.getNodeName().equals("title")) {
						it.title = node.getTextContent();
					}
					
					if (node.getNodeName().equals("content")) {
						it.description = node.getTextContent();
					}
					if (node.getNodeName().equals("media:group")){
						NodeList childNodes2 = node.getChildNodes();
						for (int b=0;b<childNodes2.getLength();b++){
							Node n2=childNodes2.item(b);
							if (n2.getNodeName().equals("yt:duration")) {
								it.duration = Integer.parseInt(n2.getAttributes()
										.getNamedItem("seconds").getNodeValue());
							}
							if (n2.getNodeName().equals("media:thumbnail")) {
								it.thumbnailUrl = n2.getAttributes()
										.getNamedItem("url").getNodeValue();
							}
							if (n2.getNodeName().equals("media:player")) {
								it.videoUrl = n2.getAttributes()
								.getNamedItem("url").getNodeValue();
							}
						}
						
					}
					if (node.getNodeName().equals("author")){
						NodeList childNodes2 = node.getChildNodes();
						for (int b=0;b<childNodes2.getLength();b++){
							Node n2=childNodes2.item(b);
							if (n2.getNodeName().equals("name")) {
								it.author=n2.getTextContent();
							}
							
						}
						
					}
					if (node.getNodeName().equals("yt:statistics")) {
						it.statistics = Integer.parseInt(node.getAttributes()
								.getNamedItem("viewCount").getNodeValue());
					}
					
					if (node.getNodeName().equals("gd:rating")) {
						it.rating = Double.parseDouble(node.getAttributes()
								.getNamedItem("average").getNodeValue());
					}
				}
			}
			items.add(it);
		}
	}

}
